<?php
defined('SYSPATH') OR die('No direct access allowed.');
/**
 * reference.php
 * @author ranuandra
 *
 * 31 Jul 2012 22:59:02
 *
 * 
 */

class Controller_Reference extends Kohana_Nasmoco
{
	public function action_index()
	{
		$pelamar = ORM::factory('mstpelamar')->where('user_id', '=', Auth_ORM::instance()->get_user()->id)->find();
		$references = $pelamar->pelamarreference->find_all();
		$view = View::factory('reference/index');
		$view->datas = $references;
		$this->template->bahasa = $view;
	}
	
	public function action_add()
	{

		$view = View::factory('reference/form');
		//print_r($pilihan);
		$this->template->container = $view;
	}
	
	public function action_save()
	{
		if(isset($_POST['submit']))
		{
			$db = Database::instance();
			$db->begin();

			try 
			{
				$pelamar = ORM::factory('mstpelamar')->where('user_id', '=', Auth_ORM::instance()->get_user()->id)->find();
				if(isset($_POST['id']))
				{
					$reference = ORM::factory('pelamarreference', Request::current()->post('id'));
					$reference->nama = Request::current()->post('nama');
					$reference->perusahaan = Request::current()->post('perusahaan');
					$reference->jabatan = Request::current()->post('jabatan');
					$reference->hubungan = Request::current()->post('hubungan');
					$reference->no_telp = Request::current()->post('no_telp');
					$reference->save();
				}
				else 
				{
					$reference = ORM::factory('pelamarreference');
					$reference->mstpelamar_id = $pelamar->id;
					$reference->created = date("Y-m-d H:i:s");
					$reference->create_data($_POST);
				}
				
				$db->commit();
				Request::current()->redirect(URL::base().'user');
				exit;
			}
			catch(ORM_Validation_Exception $e)
			{
				$db->rollback();
				$errors = $e->errors('models');
				print_r($errors);
			}
		}		
	}
	
}